//<script type='text/javascript'>

//////// INIT VALUES
var init = {
	account		: '<?= engine_get('account') ?>',
	url			: '<?= engine_get('url') ?>',
	dashboard	: <?= $DB->query_direct( "select json from moddump where ID='" . engine_get( 'dashboard.tools.id' ) . "'" ) ?>,
	lang		: '<?= LANG ?>',
	langs		: '<?= engine_get('langs') ?>'.split( ',' ),
	session		: <?= $Engine->user( "timer" ) ?>,
	logtime		: 0,
	user		: '<?= $Engine->user( "user" ) ?>',
	uid			: <?= $Engine->user() ?>
}




//////// ENGINE OBJECT
var engine = {
	path	: {
		local	: "http://<?=$_SERVER['SERVER_NAME']?>/",
		img		: "<?=CENTRAL?>img/",
		form	: "http://central.onedotonly.be/style/form/",
		js		: "http://central.onedotonly.be/js/",
	},
	thumbed		: ["social.gmap", "text.news", "media.flash"],
	type		: <?= json_encode( $DB->typelist ); ?>,
	type_r		: <?= json_encode( $DB->typelist_reverse ); ?>,
	
	type_to_opts : function() {
		
		var ref = [];
		$H( this.type ).each( function( type ){ ref.push({ key:type.value.ID, value:type.key }); });
		
		return ref;
	},
	
	generate_unique_element	: function( class ) {
			
		id = class + "." + ( $$("." + class ).length + Math.random() );
		return new Element("div", { id:id, "class": class })
	}
};


//////// TEXT REFERENCE
var txtref = {};


//////// DASHBOARD OBJECT
var dashboard = {
	ext			: {},
	
	month		: [ 'JAN', 'FEB', 'MAR', 'APR', 'MEI', 'JUN', 'JUL', 'AUG', 'SEP', 'OKT', 'NOV', 'DEC' ],
	
	seed		: function( tools ) {
		
		// Populate dashboard bar
		$("dashboard").insert( new Element( "div", { class:"lip_left" }).update( new Element( "div", { class:"in" }) ));
		$("dashboard").insert( new Element( "div", { class:"lip" }).update( init.account ));
		
		/*$("dashboard").insert(
			new Element( "input", { id:"dashboard.tools.btn", class:"button", type:"button", value:"tools" } ).observe('click', dashboard.tools.showhide )
		);*/
		$("dashboard").insert(
			new Element( "input", { id:"dashboard.btn.refresh", class:"smallbutton", type:"button", value:"refresh" } ).observe('click',function(){	xhttp.send('control.authenticate.php', {tool:'update'} ); dashboard.user.logtime(init.session); })
		);
		$("dashboard").insert(
			new Element( "input", { id:"dashboard.btn.logout", class:"smallbutton last", type:"button", value:"log out" } ).observe('click',function(){		xhttp.send('control.authenticate.php', {trg:'asset.init.cont', wtype:'asset', tool:'kill'} ); })
		);
		$("dashboard").insert( new Element( "span", { class:"info" }).update(
			"logged as: <strong>" + init.user + "</strong> &nbsp; <span id='tx.logtime'>...</span> minutes remaining"
		));
		
		$("dashboard").insert( new Element( "div", { id:"dashboard.content" }) );
		
		// Init user session
		dashboard.user.interval = setInterval("dashboard.user.logtime(-1)",60000);
		dashboard.user.logtime(-1);

	},

	tools		: {
		seed		: function(list) {
			if(list) this.list = list;
			$H(this.list).each(function(obj) {
			
				el = new Element("input", {id:obj.key, type:"button", style:"width:210px; color:#777; margin:2px;", value:obj.value.vars.descr });
				$('dashboard.tools.lip').appendChild(el);
				el.observe('click',function(){
					obj = dashboard.tools.list[this.id];
					$((obj.column)? obj.column: "column_left").insert( new Win("asset."+this.id).request(obj), {position:"after"});
					dashboard.tools.showhide();
				});
			});
		},
						
		showhide	: function(e) {
			$('dashboard.tools.lip').toggle();
			$('dashboard.tools.lip').clonePosition($('dashboard.tools.btn'),{setWidth:false, setHeight:false, offsetLeft:-5, offsetTop:(-9-$('dashboard.tools.lip').getHeight()+window.scrollY)});
		},
			
		comment		: function(message) {
			alert(message);
		}
	},
	
	user		: {
		time		: init.session,
		interval	: false,
		logtime 	: function(n) {
			dashboard.date = new Date();
			dashboard.user.time = (Math.abs(n)==n)? n : dashboard.user.time + n;
			$('tx.logtime').update(dashboard.user.time);
			if(dashboard.user.time<0) alert("time's up..."); //to be fine-tuned
		}
	},
	date		: new Date(),
	
	showhide	: function(e) {
		$('dashboard').toggleClassName("open");
		$H(win.all).each(function(obj) { obj.value.firstChild.toggle(); });
		
		//if( this.ext ) engine.ext = new this.ext;
		$('dashboard.content').update( ($('dashboard').hasClassName("open"))? engine.ext = new this.ext : "" );

		return engine.ext;
	},
	
	showextend	: function( obj ) {
		
		$('dashboard').addClassName( "open" );
		$H( win.all ).each( function(obj) { obj.value.firstChild.hide(); });
		
		$('dashboard.content').update( ext = new obj.ext( obj.vars ) );
		
		return ext;
		
	},
	
	comment		: function( message, id, fx ) {
		
		if( id && $( id ).select(".comment").length ) xhttp.gfx( message, $( id ).select(".comment")[0], fx ); 
		else {
		
			$('dashboard').insert( new Bubble( message ) );
		}
	}
}
